#include <astree/xabstractsyntaxtreeelse.h>

XAbstractSyntaxTreeElse::XAbstractSyntaxTreeElse(XTokenPtr inputToken):
    XAbstractSyntaxTreeN<2>(inputToken) {
    setName(L"else");
}

bool XAbstractSyntaxTreeElse::invokeImpl(std::shared_ptr<XVariableTable> table) {
    auto left = getChildAt(0);
    if (!left->invoke(table))
        return false;

    if (left->shouldBreak()) {
        invokeBreak = true;
        return true;
    }
    if (left->shouldReturn()) {
        result = left->getResult();
        invokeReturn = true;
        return true;
    }

    if (left->shouldInvokeSibling()) {
        auto right = getChildAt(1);
        if (!right->invoke(table))
            return false;

        if (right->shouldBreak())
            invokeBreak = right->shouldBreak();
        else if (right->shouldReturn()) {
            invokeReturn = right->shouldReturn();
            result = right->getResult();
        }
    }

    return true;
}
